// user.js
var api = require('../../api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        sexs: [{
                name: '男',
                value: '1',
                checked: 'true'
            },
            {
                name: '女',
                value: '2',
                checked: 'false'
            },
        ],
        sex: null,
    },
    // 获取radio值
    listenerRadioGroup: function (e) {
        var page = this;
        page.setData({
            sex: e.detail.value,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let type = options.type;
        var page = this;
        // 初始获取
        page.getMyOldInfo(type)
    },
    // 获取旧信息
    getMyOldInfo(type) {
        var page = this;
        app.request({
            url: api.default.userInfoSet,
            data: {
                type: type,
                token: app.globalData.token,
                // uid: wx.getStorageSync("user_info").user_id
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                if (res.code == 1) {
                    var sexVal = res.data.sex=='男'?1:2;
                    page.setData({
                        userInfo: res.data,
                        sex:sexVal,
                    });
                }
            }
        });
    },
    // 修改信息
    editorMyInfo(e) {
        var page = this;
        var warn = ""; //弹框时提示的内容
        var flag = true; //判断信息输入是否完整
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        //判断的顺序依次是：姓名-手机号
        if (e.detail.value.name == "") {
            warn = "请填写您的姓名！";
        } else if (e.detail.value.mobile == "") {
            warn = "请填写您的手机号！";
        } else if (!myreg.test(e.detail.value.mobile)) {
            warn = "请填写正确的手机号！";
        } else {
            flag = false; //若必要信息都填写，则不用弹框，且页面可以进行跳转
            var detaiVal = e.detail.value;
            var sex = page.data.sex;
            app.request({
                url: api.default.userInfoSet,
                data: {
                    type: 1,
                    token: app.globalData.token,
                    // uid: wx.getStorageSync("user_info").user_id,
                    truename: detaiVal.truename,
                    mobile: detaiVal.mobile,
                    sex: sex,
                    // parent_name: detaiVal.parent_name
                },
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (res) {
                    if (res.code == 1) {
                        wx.setStorageSync("user_info", res.data);
                        app.globalData.Wxuser = res.data;
                        wx.setStorageSync("token", res.data.token);
                        app.globalData.token = res.data.token;
                        wx.showToast({
                            title: '修改成功',
                            icon: 'success',
                            duration: 2000,
                        });
                    }
                }
            });
        }
        //如果信息填写不完整，弹出输入框
        if (flag == true) {
            wx.showModal({
                title: '提示',
                content: warn
            })
        }



    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

});